﻿using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace Jac.XkDemo.BOS.Business.PlugIn
{
    /// <summary>    
    /// 【构建插件】全事件    
    /// </summary>    
    [Description("【构建插件】全事件"), HotUpdate]
    public class AllEventDynamicWebFormBuilderPlugIn : Kingdee.BOS.Core.DynamicForm.PlugIn.AbstractDynamicWebFormBuilderPlugIn
    {
        #region event        
        /// <summary>        
        /// 控件创建事件，将控件转换为Json对象发送给前端        
        /// 通过调整输出的Json对象内容，可以控制实际输出到页面的控件样式       
        /// </summary>        
        /// <param name="e"></param>        
        public override void CreateControl(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.CreateControlEventArgs e)
        {
            PrintEventInfo("CreateControl", e);
            base.CreateControl(e);
        }
        /// <summary>        
        /// 控件创建完成后事件       
        /// </summary>        
        /// <param name="e"></param>        
        public override void AfterCreateControls(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.AfterCreateControlsEventArgs e)
        {
            PrintEventInfo("AfterCreateControls", e);
            base.AfterCreateControls(e);
        }
        /// <summary>        
        /// 主菜单创建事件（单据，账表，万能报表）        
        /// 添加、修改菜单数据结构       
        /// </summary>        
        /// <param name="bar"></param>        
        public override void CreateMainMenu(Kingdee.BOS.Core.Metadata.BarElement.BarDataManager bar)
        {
            PrintEventInfo("CreateMainMenu", bar);
            base.CreateMainMenu(bar);
        }
        /// <summary>        
        /// 列表主菜单创建后事件        
        /// </summary>        
        /// <param name="e"></param>        
        public override void AfterCreateMainMenu(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.CreateControlEventArgs e)
        {
            PrintEventInfo("AfterCreateMainMenu", e);
            base.AfterCreateMainMenu(e);
        }
        #endregion

        #region private method        

        /// <summary>        
        /// 事件执行次数计数器     
        /// </summary>      
        private int counter;

        /// <summary>        
        /// 事件执行次数计数器   
        /// </summary>   
        private int Counter { get { return ++counter; } }
        /// <summary>        
        /// 输出事件执行日志        
        /// </summary>        
        /// <param name="eventName">事件名称</param> 
        /// <param name="args">事件参数</param>  
        private void PrintEventInfo(string eventName, params object[] args)
        {
            var formId = GetFormId();
            var parameters = string.Empty;
            try
            {
                //parameters = Newtonsoft.Json.JsonConvert.SerializeObject(args);// 性能较差，可能会导致页面假死           
            }
            catch { }
            var log = string.Format("序号:{0},执行时间:{1},表单Id:{2},事件名称:{3},事件参数:{4}", Counter, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), formId, eventName, parameters);
            System.Diagnostics.Debug.Print(log);
            Kingdee.BOS.Log.Logger.Error("EVENT", log, null);
        }

        /// <summary>        
        /// 获取业务对象标识       
        /// </summary>        
        /// <returns></returns>        
        private string GetFormId()
        {
            return this.FormMetadata.BusinessInfo.GetForm().Id;
        }
        #endregion
    }
}



